<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.css" rel="stylesheet" />
        <link href="../../css/header.css" rel="stylesheet" />
	</head>

	<body>
        
        <header class="mui-bar mui-bar-nav title">
            <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: #fff;"></a>
            <a id="submit_nickname" class="mui-icon mui-icon-right-nav mui-pull-right" 
            style="color: #fff;font-size:0.875rem; line-height: 1.875rem;">完成</a>
            <h1 class="mui-title title-color">修改昵称</h1>
        </header>
        
        <div class="mui-content">
            <div class="mui-input-row" style="margin-top: 1.25rem;">
                <input id="nickname" type="text" class="mui-input-speech mui-input-clear" placeholder="输入昵称">
            </div>
        </div>
        
		<script src="../../js/mui.js"></script>
        <script src="../../js/app.js"></script>
		<script type="text/javascript">
			mui.init()
            
            mui.plusReady(function () {
               var user = app.getUserGlobalInfo();
               var txt_nickname = document.getElementById("nickname");
               txt_nickname.value = user.nickname;
               
               var submit_nickname = document.getElementById("submit_nickname");
               submit_nickname.addEventListener("tap", function(){
                   txt_nickname.blur();
                   var new_nickname = txt_nickname.value;
                   if(new_nickname.length < 1){
                       app.showToast("昵称太短", "error");
                       return false;
                   }
                   if(new_nickname.length > 8){
                       app.showToast("昵称太长", "error");
                       return false;
                   }
                   plus.nativeUI.showWaiting("正在修改昵称");
                   mui.ajax(app.serverUrl + "/user/nickname",{
                   	data:{
                   		userId:user.id,
                   		nickname:new_nickname
                   	},
                   	dataType:'json',//服务器返回json格式数据
                   	type:'post',//HTTP请求类型
                   	timeout:10000,//超时时间设置为10秒；
                   	headers:{'Content-Type':'application/json'},	              
                   	success:function(data){
                   		// // 关闭等待框
                   		plus.nativeUI.closeWaiting();
                   		if (data.code == 200) {
                   			var userInfo = data.data;
                   			app.setUserGlobalInfo(userInfo);

                            // 触发另外一个webview的自定义事件，可以使用 mui.fire()
                            var mineWebview = plus.webview.getWebviewById("mine");
                            mui.fire(mineWebview, "refresh_myface");

                   			// 页面跳转
                   			mui.openWindow("../../index.html", "index.html");
                   		} else {
                   			app.showToast(data.msg, "error");
                   		}
                   	}
                   });
               })
            })
		</script>
	</body>

</html>
